﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SharingCenter.Web.Classes.Requests
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.enums;
    using System.Xml.Linq;

    public class RequestOperations
    {
        public static void ChangeRequestStatus(Guid requestId, RequestStatus status)
        {
            using (scDataContext cxt = new scDataContext())
            {
                try
                {
                    Sc_SharedRequest t = cxt.Sc_SharedRequests.Single(d => d.RequestId == requestId);
                    t.Status = status;
                    cxt.SubmitChanges();
                }
                catch { }
            }
        }

        public static Sc_SharedRequest GetRequestById(Guid requestId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                try
                {
                    return cxt.Sc_SharedRequests.Single(d => d.RequestId == requestId);
                }
                catch { return null; }
            }
        }

        public static Sc_SharedRequest GetRequestById(Guid requestId, scDataContext cxt)
        {
            try
            {
                return cxt.Sc_SharedRequests.Single(d => d.RequestId == requestId);
            }
            catch { return null; }
        }

        public static void ChangeRequestStatus(Guid requestId, RequestStatus status, out scDataContext context)
        {
            context = new scDataContext();

            try
            {
                Sc_SharedRequest t = context.Sc_SharedRequests.Single(d => d.RequestId == requestId);
                t.Status = status;
                t.Handled = true;
            }
            catch { }
        }

        public static void ChangeRequestStatus(Guid requestId, RequestStatus status, DateTime statusChangeTime)
        {
            using (scDataContext cxt = new scDataContext())
            {
                try
                {
                    Sc_SharedRequest t = cxt.Sc_SharedRequests.Single(d => d.RequestId == requestId);
                    t.Status = status;
                    t.StatusChangeDate = statusChangeTime;
                    t.Handled = true;
                    cxt.SubmitChanges();
                }
                catch { }
            }
        }

        public static void ChangeRequestStatus(Guid requestId, RequestStatus status, scDataContext cxt, bool submitChanges)
        {
            Sc_SharedRequest t = cxt.Sc_SharedRequests.Single(d => d.RequestId == requestId);
            t.Status = status;
            t.StatusChangeDate = DateTime.Now;
            t.Handled = true;
            if (submitChanges)
            {
                cxt.SubmitChanges();
            }
        }

        public static void ChangeRequestContext(Guid requestId, XElement context, bool submitChanges)
        {
            using (scDataContext cxt = new scDataContext())
            {
                try
                {
                    Sc_SharedRequest t = cxt.Sc_SharedRequests.Single(d => d.RequestId == requestId);
                    t.Context = context;
                    if (submitChanges)
                    {
                        cxt.SubmitChanges();
                    }
                }
                catch { }
            }
        }

        public static Sc_SharedRequest GetRequest(Guid contextId, Guid requestorUserId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                var q = (from t in cxt.Sc_SharedRequests
                         where t.RequestorUserId == requestorUserId && t.ContextId == contextId
                         select t).FirstOrDefault();
                return q;
            }
        }

        public static void ChangeRequestStatus(Guid contextId, Guid requestorUserId, RequestStatus status)
        {
            using (scDataContext cxt = new scDataContext())
            {
                Sc_SharedRequest r = cxt.Sc_SharedRequests.SingleOrDefault(d => (d.RequestorUserId == requestorUserId && d.ContextId == contextId));
                if (r != null)
                {
                    
                    r.Status = status;
                    cxt.SubmitChanges();
                }
            }
        }

        public static List<Sc_SharedRequest> GetRequestsByEntityType(EntityType type, Guid contextId, RequestStatus status, LogicalOperator op)
        {
            using (scDataContext cxt = new scDataContext())
            {
                return cxt.GetRequests((int)type, contextId, (int)status, op.ToString()).ToList();
            }
        }

        public static void DeleteAllRequestInContext(Classes.enums.EntityType cxtType, Guid cxtId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                var q = GetRequestsByEntityType(cxtType, cxtId, RequestStatus.NA, LogicalOperator.NA);
                cxt.Sc_SharedRequests.DeleteAllOnSubmit(q);
            }
        }

        public static void DeleteRequest(Guid requestId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                try
                {
                    Sc_SharedRequest t = scDataContext.Instance.Sc_SharedRequests.Single(d => d.RequestId == requestId);
                    cxt.Sc_SharedRequests.DeleteOnSubmit(t);
                    cxt.SubmitChanges();
                }
                catch { }
            }
        }

        public static Sc_SharedRequest AddRequest(EntityType entityType, Guid contextId, Guid requestor, List<Guid> RecipientList, XElement context)
        {
            using (scDataContext cxt = new scDataContext())
            {
                Sc_SharedRequest t;
                try
                {
                    var q = from d in cxt.Sc_SharedRequests
                            where d.ContextId == contextId && d.RequestorUserId == requestor
                            select d;
                    t = q.First();
                    t.Status = RequestStatus.Pending;
                    t.Context = new XElement(context);
                    t.Handled = false;
                    t.Recipient = new XElement(MakeGuidList(RecipientList, "Recipients"));
                    t.StatusChangeDate = DateTime.Now;
                }
                catch
                {
                    t = new Sc_SharedRequest();
                    t.RequestId = Guid.NewGuid();
                    t.ContextType = entityType;
                    t.ContextId = contextId;
                    t.RequestorUserId = requestor;
                    t.Status = RequestStatus.Pending;
                    t.StatusChangeDate = DateTime.Now;
                    t.Handled = false;
                    t.Context = new XElement(context);
                    t.Recipient = new XElement(MakeGuidList(RecipientList, "Recipients"));
                    cxt.Sc_SharedRequests.InsertOnSubmit(t);
                }

                cxt.SubmitChanges();
                return t;
            }
        }

        public static Sc_SharedRequest AddRequest(EntityType entityType, Guid contextId, Guid requestor, Guid singleRecipient, XElement context)
        {
            Sc_SharedRequest t;
            List<Guid> recList = new List<Guid>();
            recList.Add(singleRecipient);
            using (scDataContext cxt = new scDataContext())
            {
                try
                {
                    var q = from d in cxt.Sc_SharedRequests
                            where d.ContextId == contextId && d.RequestorUserId == requestor
                            select d;
                    t = q.First();
                    t.Status = RequestStatus.Pending;
                    t.Context = context;
                    t.Recipient = MakeGuidList(recList, "Recipients");
                    t.Handled = false;
                    t.StatusChangeDate = DateTime.Now;
                }
                catch
                {
                    t = new Sc_SharedRequest();
                    t.RequestId = Guid.NewGuid();
                    t.ContextType = entityType;
                    t.ContextId = contextId;
                    t.RequestorUserId = requestor;
                    t.Status = RequestStatus.Pending;
                    t.StatusChangeDate = DateTime.Now;
                    t.Context = context;
                    t.Handled = false;
                    t.Recipient = MakeGuidList(recList, "Recipients");
                    cxt.Sc_SharedRequests.InsertOnSubmit(t);
                }

                cxt.SubmitChanges();
            }
            return t;
        }

        static XElement MakeGuidList(List<Guid> list, string elementName)
        {
            XElement element = new XElement(elementName);
            foreach(Guid id in list)
            {
                element.Add(new XElement("Entry", new XAttribute("id", id)));
            }
            return element;
        }

        ////public static Sc_SharedRequest AddRequest(EntityType entityType, Guid requestor, Guid recipient, Guid contextId, Guid contextId2, string comment)
        ////{
        ////    Sc_SharedRequest t;
        ////    try
        ////    {
        ////        t = scDataContext.Instance.Sc_SharedRequests.Single(d => (d.ContextType == entityType && d.ContextId == contextId
        ////            && d.RequestorUserId == requestor && d.RecipientUserId == recipient && d.Status != SharingCenter.Web.Classes.enums.RequestStatus.Approved));
        ////        t.Status = RequestStatus.Pending;
        ////        t.StatusChangeDate = DateTime.Now;
        ////        t.Comment = comment;
        ////    }
        ////    catch
        ////    {
        ////        t = new Sc_SharedRequest();
        ////        t.RequestId = Guid.NewGuid();
        ////        t.ContextType = entityType;
        ////        t.ContextId = contextId;
        ////        t.RequestorUserId = requestor;
        ////        t.Status = RequestStatus.Pending;
        ////        t.Comment = comment;
        ////        t.RecipientUserId = recipient;
        ////        t.ContextId2 = contextId2;
        ////        t.StatusChangeDate = DateTime.Now;
        ////        scDataContext.Instance.Sc_SharedRequests.InsertOnSubmit(t);
        ////    }

        ////    scDataContext.Instance.SubmitChanges();
        ////    return t;
        ////}
    }
}
